package com.jumi.microservice.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jumi.microservice.dto.excel.UserOrderGoodsInfo;
import com.jumi.microservice.entity.OrderGoods;
import com.jumi.microservice.entity.UserOrder;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.scheduling.annotation.Async;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author by mazhao
 * @Classname UserOrderMapper
 * @Description
 * @Date 2020/7/29 15:37
 */
@Mapper
public interface UserOrderMapper extends BaseMapper<UserOrder> {
    /**
     * 批量更新订单状态
     *
     * @param list
     * @return
     */
    @Update("<script>UPDATE user_order SET order_status = 3, " +
            " confirm_time = CASE id" +
            "<foreach collection='list' item='l' index='index'>" +
            " WHEN #{l.id} THEN #{l.confirmTime}" +
            "</foreach>" +
            "    END" +
            " WHERE id IN (" +
            "<foreach collection='list' item='l' index='index' separator=',' >" +
            "#{l.id}" +
            "</foreach>) and (order_status = 2 or(order_status = 3 and confirm_time is null))</script>")
    int batchUpdateOrderConfirmTime(List<UserOrder> list);

    /**
     * 统计某用户自购省
     *
     * @param uids
     * @return
     */
    @Select("select a.buyer_id as userId,ifnull(sum((b.goods_promotion_price - b.goods_price) * b.goods_amount),0) as amount from order_goods b " +
            "inner join user_order a " +
            "on a.id = b.order_id and b.del_flag = 0 and a.buyer_id in ( ${uids} ) and a.order_status >1 group by a.buyer_id")
    List<Map<String, Object>> selfSave(@Param("uids") String uids);

    /**
     * 更新userorder表中
     *
     * @param idStr
     * @return
     */
    @Update("update user_order set comment_flag = 1 where id in ( ${idStr} ) ")
    Integer userOrderUpdata(@Param("idStr") String idStr);

    /**
     * 获取待评价订单
     *
     * @param uid
     * @param page
     * @return
     */
    @Select("select a.* from user_order a inner join order_goods b on a.id = b.order_id and a.buyer_id = #{uid} and a.order_status = 4 and b.goods_status = 3 and a.comment_flag = 0 order by a.create_time desc")
    IPage<UserOrder> waitCommentOrder(@Param("uid") Long uid, IPage<UserOrder> page);

    @Select("<script>" + "select id from user_order where 1=1 " +
            "<if test = 'orderNo!=null' >  and order_no like CONCAT('%',#{orderNo},'%') </if>" +
            "<if test = 'createTime!=null' > and create_time BETWEEN #{createTime} AND #{endTime}</if>" +
            "<if test = 'goodsId!=null and goodsId.trim() !=&quot;&quot;' > and id in ( ${goodsId} ) </if>" +
            "</script>")
    List<Integer> getPartialShipmentOrderId(@Param("orderNo") String orderNo, @Param("createTime") String createTime, @Param("endTime") String endTime, @Param("goodsId") String goodsId);


    /**
     * 自营订单导出查询
     * @param endTime
     * @return
     */
    @Select("<script>select b.*,a.pay_time,a.remark as orderRemark,a.create_time as orderCreateTime from user_order a " +
            " inner join logistics_goods b " +
            " on a.id = b.order_id and a.confirm_time is null and a.order_status in(2,3) and b.logistics_status = 0 " +
            " and b.del_flag = 0 and b.self_flag = 1" +
            "<if test = 'list !=null'> and b.logistics_no in <foreach item='item' index='index' collection='list' " +
            " open='(' separator=',' close=')'> #{item} </foreach></if>" +
            "<if test = 'endTime!=null'> and a.create_time &lt;= #{endTime}</if>" +
            "<if test = 'warehouse !=null '> and b.real_warehouse  = #{warehouse}</if> " +
            " order by b.create_time,b.logistics_no desc</script>")
    List<UserOrderGoodsInfo> getSelfOrders(@Param("list") List<String> list,@Param("endTime") Date endTime,@Param("warehouse") Integer warehouse);

    /**
     * 全部订单导出查询
     * @param ids
     * @param startTime
     * @param endTime
     * @param goodsName
     * @return
     */
    @Select("<script>select b.*,a.pay_time,a.receiver_name,a.receiver_phone,a.receiver_address,a.order_status from user_order a inner join order_goods b on a.id = b.order_id " +
            "<if test = 'ids !=null and ids.trim()!=&quot;&quot;'> and a.id in ( ${ids} )</if>" +
            "<if test = 'orderNo !=null and orderNo.trim()!=&quot;&quot;'> and a.order_no  = #{orderNo}</if>" +
            "<if test = 'payNo !=null and payNo.trim()!=&quot;&quot;'> and a.pay_no  = #{payNo}</if>" +
            "<if test = 'startTime !=null and endTime!=null'> and a.create_time between #{startTime} and #{endTime}</if>" +
            "<if test = 'payTime !=null and payEndTime!=null'> and a.pay_time between #{payTime} and #{payEndTime}</if>" +
            "<if test = 'phoneType !=null and phoneType==1'> and a.buyer_id = #{buyerId}</if>" +
            "<if test = 'phoneType !=null and phoneType==2'> and a.receiver_phone = #{phone}</if>" +
            "<if test = 'goodsName !=null and goodsName.trim()!=&quot;&quot;'> and b.goods_name like CONCAT('%',#{goodsName},'%')</if>" +
            "<if test = 'type !=null and type == 1'> and b.supplier_id = #{supplierId}</if>" +
            "<if test = 'type !=null and type == 2'> and b.real_warehouse = #{supplierId}</if>" +
            "</script>")
    List<UserOrderGoodsInfo> getAllOrders(@Param("ids")String ids, @Param("startTime") String startTime, @Param("endTime") String endTime, @Param("goodsName")String goodsName, @Param("type")Integer type, @Param("supplierId")Integer supplierId, @Param("phoneType")Integer phoneType, @Param("buyerId")Long buyerId, @Param("phone")String phone, @Param("orderNo")String orderNo, @Param("payNo")String payNo, @Param("payTime")String payTime, @Param("payEndTime")String payEndTime);

    /**
     * 预售订单导出查询
     * @param ids
     * @param startTime
     * @param endTime
     * @param payTime
     * @param payEndTime
     * @param goodsName
     * @return
     */
    @Select("<script>select b.*,a.pay_time,a.receiver_name,a.receiver_phone,a.receiver_address,a.order_status from user_order a inner join order_goods b on a.id = b.order_id and a.order_type = 3 and b.goods_status = 0 " +
            "<if test = 'ids !=null and ids.trim()!=&quot;&quot;'> and a.id in ( ${ids} )</if>" +
            "<if test = 'startTime !=null and endTime!=null'> and a.create_time between #{startTime}and #{endTime}</if>" +
            "<if test = 'payTime !=null and payEndTime!=null'> and a.pay_time between #{payTime} and #{payEndTime}</if>" +
            "<if test = 'goodsName !=null and goodsName.trim()!=&quot;&quot;'> and b.goods_name like CONCAT('%',#{goodsName},'%')</if></script>")
    List<UserOrderGoodsInfo> getExportPreSaleOrders(@Param("ids")String ids,@Param("startTime") String startTime,@Param("endTime") String endTime,@Param("payTime")String payTime,@Param("payEndTime")String payEndTime,@Param("goodsName")String goodsName);

    /**
     * 非自营待发货订单导出查询
     * @param ids
     * @param startTime
     * @param endTime
     * @param payTime
     * @param payEndTime
     * @param goodsName
     * @return
     */
    @Select("<script>select  b.*,a.receiver_name,a.receiver_phone,a.receiver_address,a.pay_time,a.remark as orderRemark,a.buyer_id,a.create_time as orderCreateTime,a.order_status from user_order a inner join order_goods b on a.id = b.order_id and b.goods_status = 0 and b.self_flag = 0 and a.order_status in (2,3) and a.confirm_time is null" +
            "<if test = 'ids !=null and ids.trim()!=&quot;&quot;'> and a.id in ( ${ids} )</if>" +
            "<if test = 'startTime !=null and endTime!=null'> and a.create_time between #{startTime} and #{endTime}</if>" +
            "<if test = 'payTime !=null and payEndTime!=null'> and a.pay_time between #{payTime} and #{payEndTime}</if>" +
            "<if test = 'goodsName !=null and goodsName.trim()!=&quot;&quot;'> and b.goods_name like CONCAT('%',#{goodsName},'%')</if>" +
            "<if test = 'type !=null and type == 1'> and b.supplier_id = #{supplierId}</if>" +
            "<if test = 'type !=null and type == 2'> and b.real_warehouse = #{supplierId}</if>" +
            " order by b.create_time desc</script>")
    List<UserOrderGoodsInfo> getNonSelfOperatedOrders(@Param("ids")String ids,@Param("startTime") String startTime,@Param("endTime") String endTime,@Param("payTime")String payTime,@Param("payEndTime")String payEndTime,@Param("goodsName")String goodsName,@Param("type")Integer type,@Param("supplierId")Integer supplierId);

    /**
     * 待收货订单导出查询
     * @param ids
     * @param startTime
     * @param endTime
     * @param goodsName
     * @return
     */
    @Select("<script>select b.*,a.pay_time,a.receiver_name,a.receiver_phone,a.receiver_address,a.order_status from user_order a inner join order_goods b on a.id = b.order_id and b.goods_status in (1,2)" +
            "<if test = 'ids !=null and ids.trim()!=&quot;&quot;'> and a.id in ( ${ids} )</if>" +
            "<if test = 'flag !=null and flag.trim()!=&quot;&quot;'> and a.id in ( ${flag} )</if>" +
            "<if test = 'startTime !=null and endTime!=null'> and a.create_time between #{startTime} and #{endTime}</if>" +
            "<if test = 'phoneType !=null and phoneType==1'> and a.buyer_id = #{buyerId}</if>" +
            "<if test = 'phoneType !=null and phoneType==2'> and a.receiver_phone = #{phone}</if>" +
            "<if test = 'goodsName !=null and goodsName.trim()!=&quot;&quot;'> and b.goods_name like CONCAT('%',#{goodsName},'%')</if>" +
            "<if test = 'type !=null and type == 1'> and b.supplier_id = #{supplierId}</if>" +
            "<if test = 'type !=null and type == 2'> and b.real_warehouse = #{supplierId}</if>" +
            "</script>")
    List<UserOrderGoodsInfo> getGoodsReceivedOrders(@Param("ids")String ids,@Param("flag")String flag,@Param("startTime") String startTime,@Param("endTime") String endTime,@Param("goodsName")String goodsName,@Param("type")Integer type,@Param("supplierId")Integer supplierId,@Param("phoneType")Integer phoneType,@Param("buyerId")Long buyerId,@Param("phone")String phone);

    /**
     * 已完成订单导出查询
     * @param ids
     * @param startTime
     * @param endTime
     * @param goodsName
     * @return
     */
    @Select("<script>select b.*,a.pay_time,a.receiver_name,a.receiver_phone,a.receiver_address,a.order_status from user_order a inner join order_goods b on a.id = b.order_id and b.goods_status = 3 and a.order_status = 4 " +
            "<if test = 'ids !=null and ids.trim()!=&quot;&quot;'> and a.id in ( ${ids} )</if>" +
            "<if test = 'flag !=null and flag.trim()!=&quot;&quot;'> and a.id in ( ${flag} )</if>" +
            "<if test = 'startTime !=null and endTime!=null'> and a.create_time between #{startTime} and #{endTime}</if>" +
            "<if test = 'goodsName !=null and goodsName.trim()!=&quot;&quot;'> and b.goods_name like CONCAT('%',#{goodsName},'%')</if>" +
            "<if test = 'type !=null and type == 1'> and b.supplier_id = #{supplierId}</if>" +
            "<if test = 'type !=null and type == 2'> and b.real_warehouse = #{supplierId}</if>" +
            "</script>")
    List<UserOrderGoodsInfo> getCompletedListOrders(@Param("ids")String ids,@Param("flag")String flag,@Param("startTime") String startTime,@Param("endTime") String endTime,@Param("goodsName")String goodsName,@Param("type")Integer type,@Param("supplierId")Integer supplierId);

    /**
     * 售后订单导出查询
     * @param ids
     * @param startTime
     * @param endTime
     * @param goodsName
     * @return
     */
    @Select("<script>select b.*,a.pay_time,a.receiver_name,a.receiver_phone,a.receiver_address,a.order_status from user_order a inner join order_goods b on a.id = b.order_id and b.after_seal_status != 0" +
            "<if test = 'ids !=null and ids.trim()!=&quot;&quot;'> and a.id in ( ${ids} )</if>" +
            "<if test = 'startTime !=null and endTime!=null'> and a.create_time between #{startTime} and #{endTime}</if>" +
            "<if test = 'goodsName !=null and goodsName.trim()!=&quot;&quot;'> and b.goods_name like CONCAT('%',#{goodsName},'%')</if>" +
            "<if test = 'type !=null and type == 1'> and b.supplier_id = #{supplierId}</if>" +
            "<if test = 'type !=null and type == 2'> and b.real_warehouse = #{supplierId}</if>" +
            "</script>")
    List<UserOrderGoodsInfo> getAfterSaleOrders(@Param("ids")String ids,@Param("startTime") String startTime,@Param("endTime") String endTime,@Param("goodsName")String goodsName,@Param("type")Integer type,@Param("supplierId")Integer supplierId);
}
